import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Post,
  Put,
  Query,
  Req,
  UseInterceptors,
} from '@nestjs/common';
import { RoleService } from './role.service';
import { PageResult } from '../../common/result/PageResult';
import { SysRole } from './entities/role.entity';
import { RolesPageQuery } from './dto/roles.page.query';
import { Result } from '../../common/result/Result';
import { RolesForm } from './dto/roles.form';
import {
  AuthInterceptor,
  RequiredPermission,
} from '../../config/filter/auth.interceptor';

@Controller('/api/v1/roles')
export class RoleController {
  constructor(private roleService: RoleService) {}

  /**
   * 分页查询-角色
   * @param query
   */
  @Get('/page')
  @UseInterceptors(AuthInterceptor)
  async page(@Query() query: RolesPageQuery): Promise<PageResult<SysRole>> {
    const result = await this.roleService.getPage(query);
    return result;
  }

  /**
   * 角色下拉列表
   */
  @Get('/options')
  @UseInterceptors(AuthInterceptor)
  async options(): Promise<Result<SysRole[]>> {
    const result = await this.roleService.getOptions();
    return result;
  }

  /**
   * 新增角色
   * @param role
   * @param req
   */
  @Post('')
  @UseInterceptors(AuthInterceptor)
  @RequiredPermission('sys:role:add')
  async addRole(@Body() role: RolesForm, @Req() req): Promise<Result<any>> {
    const result = await this.roleService.saveRole(role, req.user);
    return result;
  }

  /**
   * 角色表单
   * @param roleId
   */
  @Get('/:roleId/form')
  @UseInterceptors(AuthInterceptor)
  async getForm(@Param('roleId') roleId: number): Promise<Result<RolesForm>> {
    const result = await this.roleService.getForm(roleId);
    return result;
  }

  /**
   * 修改角色
   * @param id
   * @param role
   * @param req
   */
  @Put('/:id')
  @UseInterceptors(AuthInterceptor)
  @RequiredPermission('sys:role:edit')
  async updateRole(
    @Param('id') id: number,
    @Body() role: RolesForm,
    @Req() req,
  ) {
    const result = await this.roleService.updateRole(role, req.user, id);
    return result;
  }

  /**
   * 删除角色
   * @param ids
   */
  @Delete('/:ids')
  @UseInterceptors(AuthInterceptor)
  @RequiredPermission('sys:role:delete')
  async deleteRoles(@Param('ids') ids: string) {
    const result = await this.roleService.deleteRoles(ids);
    return result;
  }

  /**
   * 获取角色的菜单ID集合
   * @param roleId
   */

  @Get('/:roleId/menuIds')
  @UseInterceptors(AuthInterceptor)
  async getMenuIds(@Param('roleId') roleId: string) {
    const result = await this.roleService.getMenuIds(roleId);
    return result;
  }

  /**
   * 分配菜单
   * @param roleId
   * @param menuIds
   */
  @Put('/:roleId/menus')
  @UseInterceptors(AuthInterceptor)
  async assignMenusToRole(
    @Param('roleId') roleId: number,
    @Body() menuIds: number[],
  ) {
    const result = await this.roleService.assignMenusToRole(roleId, menuIds);
    return result;
  }

  @Put('/:roleId/status')
  @UseInterceptors(AuthInterceptor)
  async updateRoleStatus(
    @Param('roleId') roleId: number,
    @Query('status') status: number,
  ) {
    const result = await this.roleService.updateRoleStatus(roleId, status);
    return result;
  }
}
